Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
fetch() catch error: show "no result" when there's no match in API

I'm trying to tell the user "No result!" when there's no match with their search. But i couldn't make it work.

enter code here

        let END_POINT = `https://api.tvmaze.com/search/shows?q=${inputValue}`
        fetch(END_POINT)
            .then(response => {
                if (response.status === 200) {
                    return response.json()
                }
                // throw new Error("No result!")
                return Promise.reject(response)
            })
            .then(data => {
                //initial cotent in the content container
                showContent(data[0])
                //forEach, display content for each click
                data.forEach(tvShow => {
                    displayContent(tvShow)
                })
            })
            .catch(error => {
                console.log(error)
            })
<div class="header">
            <div class="title">TV shows search engine</div>
            <div class="form">
                <form action="">
                    <input type="text" name="" id="" placeholder="Dune">
                    <button type="submit">Search</button>
                </form>
                <p class="error">No result!</p>
            </div>
        </div>

Any advise how to solve this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check is data.length > 0 before printing results:

let END_POINT = `https://api.tvmaze.com/search/shows?q=${inputValue}`
fetch(END_POINT)
    .then(response => {
        if (response.status === 200) {
            return response.json()
        }
        // throw new Error("No result!")
        return Promise.reject(response)
    })
    .then(data => {
        if(data.length> 0) {
                //initial cotent in the content container
            showContent(data[0])
            //forEach, display content for each click
            data.forEach(tvShow => {
                displayContent(tvShow)
            });
        } else {
            console.log("No result !");
        }
    })
    .catch(error => {
        console.log(error)
    })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!